<?php

/**
 * Status form base class.
 *
 * @package    casesonline
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 16976 2009-04-04 12:47:44Z fabien $
 */
class BaseStatusForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'               => new sfWidgetFormInputHidden(),
      'casus_id'         => new sfWidgetFormInputHidden(),
      'user_id'          => new sfWidgetFormInputHidden(),
      'started_at'       => new sfWidgetFormDateTime(),
      'stopped_at'       => new sfWidgetFormDateTime(),
      'status'           => new sfWidgetFormInput(),
      'step_id'          => new sfWidgetFormInputHidden(),
      'feedback_type'    => new sfWidgetFormInput(),
      'feedback_visible' => new sfWidgetFormInput(),
      'log_list'         => new sfWidgetFormPropelChoiceMany(array('model' => 'Step')),
    ));

    $this->setValidators(array(
      'id'               => new sfValidatorPropelChoice(array('model' => 'Status', 'column' => 'id', 'required' => false)),
      'casus_id'         => new sfValidatorPropelChoice(array('model' => 'Casus', 'column' => 'id', 'required' => false)),
      'user_id'          => new sfValidatorPropelChoice(array('model' => 'User', 'column' => 'id', 'required' => false)),
      'started_at'       => new sfValidatorDateTime(),
      'stopped_at'       => new sfValidatorDateTime(),
      'status'           => new sfValidatorString(array('max_length' => 255)),
      'step_id'          => new sfValidatorPropelChoice(array('model' => 'Step', 'column' => 'id', 'required' => false)),
      'feedback_type'    => new sfValidatorString(array('max_length' => 255)),
      'feedback_visible' => new sfValidatorString(array('max_length' => 255)),
      'log_list'         => new sfValidatorPropelChoiceMany(array('model' => 'Step', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('status[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Status';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['log_list']))
    {
      $values = array();
      foreach ($this->object->getLogs() as $obj)
      {
        $values[] = $obj->getStepId();
      }

      $this->setDefault('log_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveLogList($con);
  }

  public function saveLogList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['log_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(LogPeer::STATUS_ID, $this->object->getPrimaryKey());
    LogPeer::doDelete($c, $con);

    $values = $this->getValue('log_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new Log();
        $obj->setStatusId($this->object->getPrimaryKey());
        $obj->setStepId($value);
        $obj->save();
      }
    }
  }

}
